﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.EntityFrameworkCore;
using XZTZ.UMP.Public.Common;
using XZTZ.UMP.Public.Models.Base;
using XZTZ.UMP.WEB.Data;

namespace XZTZ.UMP.WEB.Pages.Base.DeparmentInfo
{
    public class IndexModel : PageModel
    {
        private readonly XZTZ.UMP.WEB.Data.ApplicationDbContext _context;

        public IndexModel(XZTZ.UMP.WEB.Data.ApplicationDbContext context)
        {
            _context = context;
        }

        //public IList<XZTZ.UMP.Public.Models.Base.DeparmentInfo> DeparmentInfo { get; set; }
        public PaginatedList<XZTZ.UMP.Public.Models.Base.DeparmentInfo> DeparmentInfo { get; set; }
        public string StrPropertyName { get; set; }
        public string strShortName { get; set; }
        public string DateSort { get; set; }
        public string CurrentFilter { get; set; }
        public string CurrentSort { get; set; }
        public async Task OnGetAsync(string sortOrder, string currentFilter, string searchString, int? pageIndex)
        {
            CurrentSort = sortOrder;
            StrPropertyName = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            DateSort = sortOrder == "Date" ? "date_desc" : "Date";
            //strShortName = string.IsNullOrEmpty(sortOrder) ? "short_desc" : "";
            if (searchString != null)
            {
                pageIndex = 1;
            }
            else
            {
                searchString = currentFilter;
            }
            CurrentFilter = searchString;
            IQueryable<XZTZ.UMP.Public.Models.Base.DeparmentInfo> deparmentInfoIQ = from s in _context.DeparmentInfo
                                                                                  select s;
            if (!string.IsNullOrEmpty(searchString))
            {
               deparmentInfoIQ = deparmentInfoIQ.Where(s => s.strDepartmentName.Contains(searchString) || s.dtmOperateDate.Equals(searchString));
            }

            switch (sortOrder)
            {
                case "name_desc":
                    deparmentInfoIQ = deparmentInfoIQ.OrderByDescending(s => s.strDepartmentName);
                    break;
                case "Date":
                    deparmentInfoIQ = deparmentInfoIQ.OrderBy(s => s.dtmOperateDate);
                    break;
                case "date_desc":
                    deparmentInfoIQ = deparmentInfoIQ.OrderByDescending(s => s.dtmOperateDate);
                    break;
                //case "short_desc":
                //    deparmentInfoIQ = deparmentInfoIQ.OrderByDescending(s => s.strShortName);
                //    break;
                default:
                    deparmentInfoIQ = deparmentInfoIQ.OrderBy(s => s.intIndexNo);
                    break;
            }
            int pageSize = 10;
            DeparmentInfo = await PaginatedList<XZTZ.UMP.Public.Models.Base.DeparmentInfo>.CreateAsync(deparmentInfoIQ.AsNoTracking(), pageIndex ?? 1, pageSize); 
        }
    }
}
